package com.ruoyi.monitor.service.impl;

import com.ruoyi.common.utils.js.ItemCodeOverData;
import com.ruoyi.common.utils.js.Standard;
import com.ruoyi.monitor.mapper.AutomonitoreCompareDataMapper;
import com.ruoyi.monitor.service.IBusWaterAutomonitorDaydataStatisticsService;
import com.ruoyi.utli.SegmentFormula;
import com.ruoyi.utli.service.impl.SegmentUtliServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class BusWaterAutomonitorDaydataStatisticsServiceImpl implements IBusWaterAutomonitorDaydataStatisticsService {

    @Autowired
    private AutomonitoreCompareDataMapper automonitoreDataMapper;
    @Autowired
    private SegmentFormula segmentFormula;
    /**
     * 获取自动监测数据对比统计
     *
     * @param map
     * @return
     */
    @Override
    public Map selectevaluateDaycomparestatisticList(Map map) {
        //获取断面id
        List<String> segmentIds = (List<String>) map.get("segmentIds");
        String startTime = (String) map.get("startTime");
        String endTime = (String) map.get("endTime");
        String itemId = (String) map.get("itemId");
        Map segmentlist = new HashMap();//存放断面对应数据
        //判读断面id不能为空然后根据所传入的id查询断面信息进行循环处理
        if (segmentIds != null && segmentIds.size() > 0) {
            List<String> segmentList = new ArrayList<>();
            List<String> segmentmonth = new ArrayList<>();//存放循环处理过的当期断面级别
            List<String> segmentlastmonth = new ArrayList<>();//存放循环处理过的同比的断面级别
            List<String> segmentlastyear = new ArrayList<>();//存放循环处理过的环比的断面级别

            Map WqBasSegmentNamemap = new HashMap();//存放断面id进行查询断面信息
            WqBasSegmentNamemap.put("segmentIds", segmentIds);
            //查询出断面信息
            List<Map> SegmentNamelist = automonitoreDataMapper.GetSegmentNameByids(WqBasSegmentNamemap);
            if (SegmentNamelist != null && SegmentNamelist.size() > 0) {
                SegmentNamelist = SegmentNamelist.stream().sorted(Comparator.comparing(s -> String.valueOf(s.get("SEGMENTSORT")))).collect(Collectors.toList());
                for (Map s : SegmentNamelist) {
                    segmentList.add(String.valueOf(s.get("SEGMENTNAME")));
                    map.put("segmentId",s.get("ID"));
                    if(itemId!=null&&!"0".equals(itemId))
                    {
                        //默认传参当期查询
                        map.put("yearOnYearType","data");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistdata =  automonitoreDataMapper.segmentItemCodeAVGValue(map);
                        //获取数据后进行数据计算水质
                        //判读如果没有获取到数据，添加一条为空的数据
                        if(segmentItemCodeAVGValuelistdata!=null&&segmentItemCodeAVGValuelistdata.size()>0){
                            Object avgValue = segmentItemCodeAVGValuelistdata.get(0).get("AVGVALUE");//获取当期因子平均值
                            segmentmonth.add(String.valueOf(avgValue));
                        }else {
                            segmentmonth.add(null);
                        }
                        //获取同比上一个月的数据
                        map.put("yearOnYearType","monthData");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistmonthData = automonitoreDataMapper.segmentItemCodeAVGValue(map);

                        //获取数据后进行数据计算水质
                        if(segmentItemCodeAVGValuelistmonthData!=null&&segmentItemCodeAVGValuelistmonthData.size()>0){
                            Object avgValue = segmentItemCodeAVGValuelistmonthData.get(0).get("AVGVALUE");//获取同比因子平均值
                            segmentlastmonth.add(String.valueOf(avgValue));
                        }else {
                            segmentlastmonth.add(null);
                        }
                        //获取环比上一个年的数据
                        map.put("yearOnYearType","yearData");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistyearData =  automonitoreDataMapper.segmentItemCodeAVGValue(map);
                        //获取数据后进行数据计算水质
                        if(segmentItemCodeAVGValuelistyearData!=null&&segmentItemCodeAVGValuelistyearData.size()>0){
                            Object avgValue = segmentItemCodeAVGValuelistyearData.get(0).get("AVGVALUE");//获取环比因子平均值
                            segmentlastyear.add(String.valueOf(avgValue));
                        }else {
                            segmentlastyear.add(null);
                        }
                    }
                    else
                    {

                        //存放当前断面的最大水质级别
                        Integer dataSegmentMAX=null;
                        //工具类(获取对应因子的计算标准)
                        SegmentUtliServiceImpl SegmentUtliServiceImpl=new SegmentUtliServiceImpl();
                        //引入公共计算返回等级
                        ItemCodeOverData itemCodeOverData=new ItemCodeOverData();
                        //默认传参当期查询
                        map.put("yearOnYearType","data");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistdata = automonitoreDataMapper.segmentItemCodeAVGValue(map);
                        //获取数据后进行数据计算水质
                        if(segmentItemCodeAVGValuelistdata!=null&&segmentItemCodeAVGValuelistdata.size()>0){
                            for(Map segmentItemCodeAVGValuelists:segmentItemCodeAVGValuelistdata){
                                //断面id
                                Object segmentID = segmentItemCodeAVGValuelists.get("SEGMENTID");
                                //断面名称
                                Object SegmentNamedata = segmentItemCodeAVGValuelists.get("SEGMENTNAME");
                                //监测因子名称
                                Object ItemName = segmentItemCodeAVGValuelists.get("ITEMNAME");
                                //监测因子编码
                                Object ItemCode = segmentItemCodeAVGValuelists.get("ITEMCODE");
                                //平均值
                                Object avgValue = segmentItemCodeAVGValuelists.get("AVGVALUE");
                                //因子 项目分类(0地表水/1地下水)
                                Object ItemCategroy = segmentItemCodeAVGValuelists.get("ITEMCATEGROY");
                                //调用工具类返回计算标准数据
                                Map segmentStandardValuemap=new HashMap();
                                segmentStandardValuemap.put("ItemCode",ItemCode);
                                segmentStandardValuemap.put("ID",segmentID);
                                List<Standard> standards = segmentFormula.segmentStandardValue(segmentStandardValuemap);
                                Integer integer = ItemCodeOverData.GetSingleType(Double.valueOf(String.valueOf(avgValue)), standards, String.valueOf(ItemCode),String.valueOf(avgValue),Integer.valueOf(String.valueOf(ItemCategroy)));
                                //获取对应断面级别后进行对比获取整个断面最大的等级
                                if(integer!=null){
                                    if(dataSegmentMAX!=null){
                                        if( integer>dataSegmentMAX){
                                            dataSegmentMAX=integer;
                                        }
                                    }else {
                                        dataSegmentMAX=integer;
                                    }
                                }

                            }
                        }
                        //判断是否有返回的断面等级数据没有默认为空
                        if(dataSegmentMAX!=null){
                            //等级不为空的话进行转换为汉字
                            segmentmonth.add(String.valueOf(dataSegmentMAX));
                        }else {
                            segmentmonth.add(null);
                        }
                        //存放同比断面的最大水质级别
                        Integer monthDataSegmentMAX=null;
                        //获取同比上一个月的数据
                        map.put("yearOnYearType","monthData");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistmonthData = automonitoreDataMapper.segmentItemCodeAVGValue(map);
                        //获取数据后进行数据计算水质
                        if(segmentItemCodeAVGValuelistmonthData!=null&&segmentItemCodeAVGValuelistmonthData.size()>0){
                            for(Map segmentItemCodeAVGValuelists:segmentItemCodeAVGValuelistmonthData){
                                //断面id
                                Object segmentID = segmentItemCodeAVGValuelists.get("SEGMENTID");
                                //断面名称
                                Object SegmentNamedata = segmentItemCodeAVGValuelists.get("SEGMENTNAME");
                                //监测因子名称
                                Object ItemName = segmentItemCodeAVGValuelists.get("ITEMNAME");
                                //监测因子编码
                                Object ItemCode = segmentItemCodeAVGValuelists.get("ITEMCODE");
                                //平均值
                                Object avgValue = segmentItemCodeAVGValuelists.get("AVGVALUE");
                                //因子 项目分类(0地表水/1地下水)
                                Object ItemCategroy = segmentItemCodeAVGValuelists.get("ITEMCATEGROY");
                                //调用工具类返回计算标准数据
                                Map segmentStandardValuemap=new HashMap();
                                segmentStandardValuemap.put("ItemCode",ItemCode);
                                segmentStandardValuemap.put("ID",segmentID);
                                List<Standard> standards = segmentFormula.segmentStandardValue(segmentStandardValuemap);
                                Integer integer = ItemCodeOverData.GetSingleType(Double.valueOf(String.valueOf(avgValue)), standards, String.valueOf(ItemCode),String.valueOf(avgValue),Integer.valueOf(String.valueOf(ItemCategroy)));
                                //获取对应断面级别后进行对比获取整个断面最大的等级
                                if(integer!=null){
                                    if(monthDataSegmentMAX!=null){
                                        if( integer>monthDataSegmentMAX){
                                            monthDataSegmentMAX=integer;
                                        }
                                    }else {
                                        monthDataSegmentMAX=integer;
                                    }
                                }

                            }
                        }
                        //判断是否有返回的断面等级数据没有默认为空
                        if(monthDataSegmentMAX!=null){
                            //等级不为空的话进行转换为汉字
                            segmentlastmonth.add(String.valueOf(monthDataSegmentMAX));
                        }else {
                            segmentlastmonth.add(null);
                        }

                        //存放环比断面的最大水质级别
                        Integer yearDataSegmentMAX=null;
                        //获取环比上一个年的数据
                        map.put("yearOnYearType","yearData");
                        //获取当期的监测数据
                        List<Map> segmentItemCodeAVGValuelistyearData = automonitoreDataMapper.segmentItemCodeAVGValue(map);
                        //获取数据后进行数据计算水质
                        if(segmentItemCodeAVGValuelistyearData!=null&&segmentItemCodeAVGValuelistyearData.size()>0){
                            for(Map segmentItemCodeAVGValuelists:segmentItemCodeAVGValuelistyearData){
                                //断面id
                                Object segmentID = segmentItemCodeAVGValuelists.get("SEGMENTID");
                                //断面名称
                                Object SegmentNamedata = segmentItemCodeAVGValuelists.get("SEGMENTNAME");
                                //监测因子名称
                                Object ItemName = segmentItemCodeAVGValuelists.get("ITEMNAME");
                                //监测因子编码
                                Object ItemCode = segmentItemCodeAVGValuelists.get("ITEMCODE");
                                //平均值
                                Object avgValue = segmentItemCodeAVGValuelists.get("AVGVALUE");
                                //因子 项目分类(0地表水/1地下水)
                                Object ItemCategroy = segmentItemCodeAVGValuelists.get("ITEMCATEGROY");
                                //调用工具类返回计算标准数据
                                Map segmentStandardValuemap=new HashMap();
                                segmentStandardValuemap.put("ItemCode",ItemCode);
                                segmentStandardValuemap.put("ID",segmentID);
                                List<Standard> standards = segmentFormula.segmentStandardValue(segmentStandardValuemap);
                                Integer integer = ItemCodeOverData.GetSingleType(Double.valueOf(String.valueOf(avgValue)), standards, String.valueOf(ItemCode),String.valueOf(avgValue),Integer.valueOf(String.valueOf(ItemCategroy)));
                                //获取对应断面级别后进行对比获取整个断面最大的等级
                                if(integer!=null){
                                    if(yearDataSegmentMAX!=null){
                                        if( integer>yearDataSegmentMAX){
                                            yearDataSegmentMAX=integer;
                                        }
                                    }else {
                                        yearDataSegmentMAX=integer;
                                    }
                                }

                            }
                        }
                        //判断是否有返回的断面等级数据没有默认为空
                        if(yearDataSegmentMAX!=null){
                            //等级不为空的话进行转换为汉字
                            segmentlastyear.add(String.valueOf(yearDataSegmentMAX));
                        }else {
                            segmentlastyear.add(null);
                        }
                    }
                }
            }
            //断面名称存放
            segmentlist.put("segment", segmentList);
            //存放当期数据
            segmentlist.put("month", segmentmonth);
            //存放环比 上一个月的
            segmentlist.put("lastmonth", segmentlastmonth);
            //同比 上一年的
            segmentlist.put("lastyear", segmentlastyear);
        }
        return segmentlist;
    }
}
